package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsInspectTaskDetailMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsInspectTaskDetailMapperImpl;
import cc.rengu.igas.bsps.common.entity.InspectTaskDetail;
import cc.rengu.igas.bsps.common.enums.InspectStatusEnum;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.BankManagerService;
import cc.rengu.igas.bsps.facade.request.MchntInspectInfoAuditRequest;
import cc.rengu.igas.bsps.facade.response.MchntInspectInfoAuditResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020.04.09
 */
public class MchntInspectInfoAuditService extends BankManagerService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntInspectInfoAuditRequest mchntInspectInfoAuditRequest = new MchntInspectInfoAuditRequest();
        ConvertUtil.convertOutput(mchntInspectInfoAuditRequest);
        return mchntInspectInfoAuditRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntInspectInfoAuditRequest mchntInspectInfoAuditRequest = (MchntInspectInfoAuditRequest) request;
        /*获取请求参数进行：法人行号，用户标识，拓展经理工号，巡检任务编号，商户号，审核状态，审核拒绝原因*/
        String instId = mchntInspectInfoAuditRequest.getHeader().getInstId();
        String userId = mchntInspectInfoAuditRequest.getUserId();
        String managerId = mchntInspectInfoAuditRequest.getManagerId();
        String inspectTaskNo = mchntInspectInfoAuditRequest.getInspectTaskNo();
        String mchntNo = mchntInspectInfoAuditRequest.getMchntNo();
        String checkStatus = mchntInspectInfoAuditRequest.getCheckStatus();
        String inspectRefuseReason = mchntInspectInfoAuditRequest.getInspectRefuseReason();
        /*字段必填校验失败*/
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(managerId) || StringUtil.isEmptyOrNull(inspectTaskNo)
                || StringUtil.isEmptyOrNull(checkStatus)) {
            rglog.error("巡检商户信息审核必填性校验失败");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        if (InspectStatusEnum.REFUSED_STATUS.getStatusCode().equals(checkStatus) && StringUtil.isEmptyOrNull(inspectRefuseReason)) {
            rglog.error("巡检拒绝时拒绝原因不能为空");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*更新商户巡检任务详情信息*/
        BspsInspectTaskDetailMapper bspsInspectTaskDetailMapper = new BspsInspectTaskDetailMapperImpl();
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        /*法人行号赋值*/
        inspectTaskDetail.setInstId(instId);
        /*商户号赋值*/
        inspectTaskDetail.setMchntNo(mchntNo);
        /*巡检任务编号赋值*/
        inspectTaskDetail.setInspectTaskNo(inspectTaskNo);
        /*巡检状态赋值*/
        inspectTaskDetail.setInspectStatus(checkStatus);
        /*巡检拒绝原因赋值*/
        if (InspectStatusEnum.REFUSED_STATUS.getStatusCode().equals(checkStatus)) {
            inspectTaskDetail.setInspectRefuseReason(inspectRefuseReason);
        }
        /*最后操作人赋值*/
        inspectTaskDetail.setLastOprId(userId);
        /*最后操作时间赋值*/
        inspectTaskDetail.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = bspsInspectTaskDetailMapper.CheckBspsInspectTaskDetailByMchntNoAndTaskNo(inspectTaskDetail);
        if (0 != iReturnCode) {
            rglog.error("更新商户巡检任务信息失败,iReturnCode:<{}>,mchntNo:<{},inspectTaskNo:<{}>", iReturnCode, mchntNo, inspectTaskNo);
            throw new BizException(RespCodeEnum.UPDATE_INSPECT_TASK_DETAIL_ERROR.getRespCode(), RespCodeEnum.UPDATE_INSPECT_TASK_DETAIL_ERROR.getRespDesc());
        }

        /* 拼装响应结果 */
        MchntInspectInfoAuditResponse mchntInspectInfoAuditResponse = new MchntInspectInfoAuditResponse();
        BizResponse<MchntInspectInfoAuditResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntInspectInfoAuditResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntInspectInfoAuditResponse mchntInspectInfoAuditResponse = (MchntInspectInfoAuditResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntInspectInfoAuditResponse);
    }
}
